package com.tivo.jipviewer;

import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.NumberFormat;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import com.tivo.jipviewer.MethodViewer.MyDoubleRenderer;

public class BundleViewer extends Container implements ChangeListener, MouseListener {

	private JTable bundleTable;
	private JTable serviceTable;
	private JipRun run;
	private JLabel label;

	private ValueModel<JipBundle> model = new ValueModel<JipBundle>();

	private BundleRowTableModel   bundleModel  = new BundleRowTableModel();
	private ServiceRowTableModel  serviceModel = new ServiceRowTableModel();

	BundleViewer(JipRun _run, ValueModel<JipBundle> _model){
		this.run = _run;
		this.model = _model;

		model.addChangeListener(this);

		label = new JLabel("Bundle");

		bundleTable = makeTableForBundleRows(bundleModel, BundleRowTableModel.SV_WIDTH);
		bundleTable.addMouseListener(this);
		
		serviceTable = makeTableForBundleRows(serviceModel, ServiceRowTableModel.SV_WIDTH);
		serviceTable.addMouseListener(this);

		add(new JScrollPane(bundleTable));
        add(new JSeparator());
        add(new JScrollPane(serviceTable));
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

	}

	public JTable getTable(){
		return bundleTable;
	}

	 // A renderer that displays doubles with a fixed number of decimal columns
//    static class MyDoubleRenderer extends DefaultTableCellRenderer {
//        NumberFormat mFormat;
//
//        MyDoubleRenderer() {
//            super();
//            setHorizontalAlignment(SwingConstants.RIGHT);
//        }
//
//        public void setValue(Object value) {
//            if (mFormat == null) {
//                mFormat = (NumberFormat) NumberFormat.getInstance().clone();
//                mFormat.setMinimumFractionDigits(2);
//            }
//            setText(mFormat.format(value));
//        }
//    };



	static JTable makeTableForBundleRows(TableModel model, int[] cellSizeArray) {
        TableSorter sorter = new TableSorter(model);
        MultiLineCellRenderer cellRenderer = new MultiLineCellRenderer(SwingConstants.LEFT, SwingConstants.CENTER);

        JTable table = new JTable(sorter);
        sorter.setTableHeader(table.getTableHeader());

        TableColumnModel colModel = table.getColumnModel();
        int nCol = model.getColumnCount();
        for (int iCol=0; iCol < nCol; iCol++) {
            TableColumn col = colModel.getColumn(iCol);
            if (iCol < cellSizeArray.length) {
                col.setPreferredWidth(cellSizeArray[iCol]);
                col.setCellRenderer(cellRenderer);
            }
        }
        table.doLayout();

//        table.setDefaultRenderer(Double.class, new MyDoubleRenderer());
//        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        return table;
    }
	
	@Override
	public void changed(Object source) {

		JipBundle bundle = model.getValue();
		bundleModel.clear();
		serviceModel.clear();

		if (bundle != null){

			bundleModel.add(bundle);
			serviceModel.add(bundle.getServiceList());

		}

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
